<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <script src="static/js/algorithmic/list.js"></script>
    <script src="static/js/algorithmic/stack.js"></script>
    <script src="static/js/algorithmic/linkedlist.js"></script>
    <script>
        var list = new List();
        var john = "john";
        var tom = "tom";
        var jack = "jack";
        var lucy = "lucy";
        var mary = "mary";

        list.append(john);
        list.append(john);
        list.append(tom);
        list.append(john);
        list.append(john);
        list.append(john);
        list.append(john);
        list.append(jack);
        list.append(john);
        list.append(lucy);

        list.append(john);

        list.insert(mary, 2);
        // console.log(list);

        // for (list.front(); list.cur() < list.length(); list.next()) {
        //     console.log(list.getElement())
        // }

        //把list所有john都删掉
        //遍历数组，把里面所有值等于john的删除
        // for (var i = 0; i < list.dataStore.length; i++) {
        //     if (list.dataStore[i] === "john") {
        //         list.dataStore.splice(i, 1);
        //         i--;
        //         // console.log(list.dataStore.length)
        //     }
        // }

        list.foreach(function(index, element) { //封装循环的方法
            if (element == "john") {
                list.removeCur();
            }
        })
        console.log(list.dataStore);

        //如何将一个数字转换另外一个进制表现

        var mulBase = function(num, base) {
            var s = new Stack();
            do {
                s.push(num % base); //余数
                num = Math.floor(num / base); //处于进制位,向下取整的数
            } while (num > 0);
            var converted = "";
            //先进后出,后进先出
            while (s.length() > 0) {
                converted = converted + s.pop();
            }
            return converted;
        }
        console.log(mulBase(4, 2));

        //回文   
        //abcdcba
        //abdddefa

        //写一个函数验证一个单词是否是回文(栈)
        var huiwen = function(s) {
            var stack = new Stack();
            //压栈
            for (var i = 0; i < s.length; i++) {
                stack.push(s[i]);
            }
            //出栈
            var ans = "";
            for (var i = 0; i < s.length; i++) {
                ans = ans + stack.pop();
            }
            return s == ans;
        }
        console.log(huiwen("abcdcba"));
        console.log(huiwen("bcdc3b"));


        //列表
        var linkedList = new LinkedList();
        linkedList.insert(john);
        linkedList.insert(tom);
        linkedList.insert(mary);
        linkedList.delete(tom);

        console.log(linkedList);
    </script>
</body>

</html>